package com.ssm.basis.service.util;

import com.ssm.basis.service.config.SsmContext;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.Map;

/**
 * 业务应用上下文
 */
public abstract class SsmContextHolder {
    static SsmContext ssmContext;

    /**
     * 子线程也可以获取到当前上下文对象
     */
    final static ThreadLocal<Map<String, Object>> THREADLOCAL = new InheritableThreadLocal<Map<String, Object>>() {
        @Override
        protected Map<String, Object> initialValue() {
            return new HashMap<>();
        }
    };

    public static String getMessage() {
        return ssmContext.getMessage();
    }

    public static void setMessage(String message) {
        ssmContext.setMessage(message);
    }

    /**
     * 获取上下文中的信息
     */
    public static Map<String, Object> getContext() {
        return THREADLOCAL.get();
    }

    /**
     * 清空上下文
     */
    public static void clear() {
        THREADLOCAL.remove();
    }

    @Autowired
    void setSsmContext(SsmContext ssmContext) {
        SsmContextHolder.ssmContext = ssmContext;
    }
}